package com.live.dengdengtv.fragment.hall;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.DianboActivity;
import com.live.dengdengtv.bean.gson.CategoryList_DB;
import com.live.dengdengtv.bean.gson.VideodInfoList_DB;
import com.live.dengdengtv.fragment.BaseFragment;
import com.live.dengdengtv.net.DianboNetTask;
import com.live.dengdengtv.view.DialogWaiting;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by wyf on 2016/8/5.
 */
public class ChannelFragment extends BaseFragment implements View.OnClickListener{

    private static final String TAG = "ChannelFragment";

    private TextView joke_tv;
    private TextView star_tv;
    private TextView live_tv;
    private TextView follow_tv;
    private TextView currentTab_tv;
    private String currentCategoryId = "";

    private View contentView;
    private LinearLayout channel_other_ll;
    private LinearLayout channel_first_ll;

    private ListView listView;
    private BaseAdapter adapter;

    private Map<String, ArrayList<VideodInfoList_DB.VideoFileInfo>> ListData = new HashMap<>();
    private DialogWaiting progressDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (contentView == null) {

            contentView = inflater.inflate(R.layout.frag_channel, container, false);

            channel_first_ll = (LinearLayout) contentView.findViewById(R.id.channel_first_ll);
            channel_other_ll = (LinearLayout) contentView.findViewById(R.id.channel_first_ll);

            //频道分类
            joke_tv = (TextView) contentView.findViewById(R.id.joke_tv);
            star_tv = (TextView) contentView.findViewById(R.id.star_tv);
            live_tv = (TextView) contentView.findViewById(R.id.live_tv);
            follow_tv = (TextView) contentView.findViewById(R.id.follow_tv);

            channel_first_ll.setOnClickListener(this);
            joke_tv.setOnClickListener(this);
            star_tv.setOnClickListener(this);
            live_tv.setOnClickListener(this);
            follow_tv.setOnClickListener(this);

            listView = (ListView) contentView.findViewById(R.id.listView);
            adapter = new VideoAdapter(getContext());
            listView.setAdapter(adapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ArrayList<VideodInfoList_DB.VideoFileInfo> categoryData =  ListData.get(currentCategoryId);
                    Intent intent = new Intent(getActivity(), DianboActivity.class);
                    intent.putExtra("fileId", categoryData.get(position).fileId);
                    startActivity(intent);
                }
            });

            getCategoryList();

        }
        return contentView;
    }

    private void getCategoryList() {

        progressDialog = DialogWaiting.show(getActivity());
        DianboNetTask.getCategoryList(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dimiss();
                Log.e(TAG, e.toString());
            }
            @Override
            public void onResponse(String response, int id) {
                progressDialog.dimiss();
                Log.e(TAG, response);

                CategoryList_DB categoryList = new Gson().fromJson(response, CategoryList_DB.class);
                if(categoryList.code == 0){
                    addCategory(categoryList.data);
                }else {
                    Log.e(TAG, response);
                }
            }
        });

    }

    private void addCategory(ArrayList<CategoryList_DB.CategoryInfo> videoFileSet){
        if(videoFileSet.size()<=0) return;
        for (CategoryList_DB.CategoryInfo videoInfo : videoFileSet){

            if(videoInfo.name.contains("噔噔出品")) {//||videoInfo.id.equals("26978")噔噔出品
                channel_first_ll.setTag(videoInfo.id);
                ListData.put(videoInfo.id, new ArrayList<VideodInfoList_DB.VideoFileInfo>());
                currentCategoryId = videoInfo.id;
                getVideoInfoList(videoInfo.id);//获取噔噔出品数据
            }else if(videoInfo.name.equals("搞笑")) {
                joke_tv.setTag(videoInfo.id);
                ListData.put(videoInfo.id, new ArrayList<VideodInfoList_DB.VideoFileInfo>());
            }else if(videoInfo.name.equals("明星")) {
                star_tv.setTag(videoInfo.id);
                ListData.put(videoInfo.id, new ArrayList<VideodInfoList_DB.VideoFileInfo>());
            }else if(videoInfo.name.equals("现场")) {
                live_tv.setTag(videoInfo.id);
                ListData.put(videoInfo.id, new ArrayList<VideodInfoList_DB.VideoFileInfo>());
            }else if(videoInfo.name.equals("关注")) {
                follow_tv.setTag(videoInfo.id);
                ListData.put(videoInfo.id, new ArrayList<VideodInfoList_DB.VideoFileInfo>());
            }

//            TextView channel_item = (TextView) View.inflate(getContext(),R.layout.channel_item,null);
//            channel_item.setText(videoInfo.name);
//            channel_item.setTag(videoInfo.id);
//            channel_other_ll.addView(channel_item);
        }
    }
    @Override
    public void onClick(View v) {
        String id = (String) v.getTag();
        if(id != null){

            if(currentTab_tv != null){
                currentTab_tv.setTextColor(0xFF333333);
            }
            if(v.getId() == R.id.channel_first_ll){
                currentTab_tv = null;
            }else {
                currentTab_tv = (TextView) v;
                currentTab_tv.setTextColor(0xFFED242C);
            }

            currentCategoryId = id;
            getVideoInfoList(id);
        }
    }
    private void getVideoInfoList(String classId){
        DianboNetTask.getSerchVideo(classId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e(TAG, response);

                VideodInfoList_DB videoList = new Gson().fromJson(response, VideodInfoList_DB.class);
                if(videoList.code == 0){
                    ArrayList<VideodInfoList_DB.VideoFileInfo> categoryData =  ListData.get(currentCategoryId);
                    categoryData.clear();
                    if(videoList.totalCount>0){
                        categoryData.addAll(videoList.fileSet);
                    }
                    adapter.notifyDataSetChanged();
                }else {
                    Log.e(TAG, response);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public class VideoAdapter extends BaseAdapter{
        private LayoutInflater mInflater;

        public VideoAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            ArrayList<VideodInfoList_DB.VideoFileInfo> categoryData =  ListData.get(currentCategoryId);
            return categoryData==null ? 0:categoryData.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.channel_videoitem, null);
                holder = new ViewHolder();
                /*得到各个控件的对象*/
                holder.head_iv = (ImageView) convertView.findViewById(R.id.head_iv);
                holder.video_bg_iv = (ImageView)convertView.findViewById(R.id.video_bg_iv);
                holder.like_tv = (TextView) convertView.findViewById(R.id.like_tv);

                convertView.setTag(holder); //绑定ViewHolder对象
            }
            else {
                holder = (ViewHolder) convertView.getTag(); //取出ViewHolder对象
            }

            ArrayList<VideodInfoList_DB.VideoFileInfo> categoryData =  ListData.get(currentCategoryId);
            if(categoryData != null ){
                String imageUrl = categoryData.get(position).imageUrl;
                if(!TextUtils.isEmpty(imageUrl)){
//                    ImageLoader.getInstance().displayImage(imageUrl, holder.video_bg_iv);
                    Glide.with(ChannelFragment.this)
                            .load(imageUrl)
                            .into(holder.video_bg_iv);
                }
            }


            return convertView;
        }
    }

    /*存放控件 的ViewHolder*/
    public final class ViewHolder {
        public ImageView head_iv;//头像
        public ImageView video_bg_iv;
        public TextView like_tv;
    }

}
